package windowtips

import (
	"fmt"
	"github.com/mattn/go-gtk/gdk"
	"github.com/mattn/go-gtk/glib"
	"github.com/mattn/go-gtk/gtk"
	"io/ioutil"
	"jkgo/jk/jklog"
	"math/rand"
	"time"
)

type WindowTips struct {
	Stype   string
	Content string
	window  *gtk.Window
}

func generateBackName() string {
	s1 := rand.NewSource(time.Now().Unix())
	r1 := rand.New(s1)
	index := r1.Intn(3)
	basepath := "/home/v/Nutstore Files/Nutstore/localuse/"
	switch index {
	case 0:
		basepath += "backgroud/s"
	case 1:
		basepath += "pics/t/1"
	case 2:
		basepath += "pics/t/14"
	case 3:
		basepath += "pics/t/15"
	default:
		basepath += "pics/t/15"
	}
	jklog.L().Debugln("read from dir ", basepath)
	files, _ := ioutil.ReadDir(basepath)
	findex := r1.Intn(len(files) - 1)
	basepath += "/" + files[findex].Name()
	jklog.L().Debugln("read from file ", basepath)

	return basepath
}

func NewWindowTips(stype, content string, args *[]string) (*WindowTips, error) {
	wt := &WindowTips{
		Stype:   stype,
		Content: content,
	}

	gtk.Init(args)
	window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
	window.SetPosition(gtk.WIN_POS_CENTER)
	window.SetTitle("JKTips")
	window.SetIconName("gtk-dialog-info")
	window.SetSizeRequest(1280, 960)
	window.Connect("destroy", func(ctx *glib.CallbackContext) {
		fmt.Println("got destroy!", ctx.Data().(string))
		gtk.MainQuit()
	}, "foo")

	box := gtk.NewHPaned()

	x, y := window.GetSize()
	jklog.L().Debugln(x, ", ", y)
	vbox := gtk.NewHBox(false, 1)
	image := gtk.NewImageFromFile(generateBackName())
	//_, iy := image.GetSizeRequest()
	align := gtk.NewAlignment(0, 0, 0, 0)
	align.SetSizeRequest(x/2, y/2)
	align.Add(image)

	label := gtk.NewLabel(wt.Content)
	label.ModifyText(gtk.STATE_SELECTED, gdk.NewColorRGB(10, 10, 255))
	label.ModifyFontEasy("DejaVu Serif 26")
	label.SetLineWrap(true)

	vbox.Add(align)
	vbox.Add(label)
	vbox.ModifyBG(gtk.STATE_SELECTED, gdk.NewColorRGB(100, 50, 50))

	box.Pack1(vbox, false, false)
	window.Add(box)

	wt.window = window

	return wt, nil
}

func (wt *WindowTips) Show() {
	wt.window.ShowAll()
	gtk.Main()
}

func (wt *WindowTips) ShowFull() {
	wt.window.Fullscreen()
	wt.window.ShowAll()
	gtk.Main()
}
